<?php

namespace app\notify\controller;

use app\BaseController;

class WechatNotify extends BaseController
{
    /** 
     * 微信APP异步回调 
     */
    public function wxapp() {
        $weChat = addons_action('WeChat', 'AppPay', 'init');
        try {
            $arr = $weChat->verifyNotify();
            $verifyService = new \app\service\order\Verify();
            $verifyService->verify($arr['out_trade_no'], 'wxpay', $arr, $arr['transaction_id'], $arr['total_fee']);
        } catch (\Exception $e) {
            \think\facade\Log::error($e->getMessage());
            $weChat->NotifyProcess(false, '支付异常');
        }
       
        $weChat->NotifyProcess(true, 'OK');
    }

    public function wxappBack()
    {
    }

    /**
     * 微信小程序异步回调
     * @created 2020/2/17 21:57
     */
    public function mini() {

        try {
            $weChat = addons_action('WeChat', 'MiniPay', 'initWechatPay');
            $arr = $weChat->getNotify();
            $verifyService = new \app\service\order\Verify();
            $verifyService->verify($arr['out_trade_no'], 'minipay', $arr, $arr['transaction_id'], $arr['total_fee']);
        } catch (\Exception $e) {
            \think\facade\Log::error($e->getMessage());
            return "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
        }     
        return "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
    }

    /**
     * 微信公众号异步回调
     */
    public function wxcode() {
        try {
            $weChat = addons_action('WeChat', 'CodePay', 'initWechatPay');
            $arr = $weChat->getNotify();
            $verifyService = new \app\service\order\Verify();
            $verifyService->verify($arr['out_trade_no'], 'wxcode', $arr, $arr['transaction_id'], $arr['total_fee']);
        } catch (\Exception $e) {
            return "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
        }     
        return "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
    }

    /**
     * 微信小程序支付v3
     * @author mitu
     * @date   2023-12-11
     * @return [type]     [description]
     */
    public function miniV3() {
        try {
            $weChat = addons_action('WeChat', 'MiniPayV3', 'initWechatPay');
            $result = $weChat->notify();
            $arr = json_decode($result['result'], true);
            \think\facade\Log::info($arr);
            $verifyService = new \app\service\order\Verify();
            $payerTotal = $arr['amount']['payer_total'];
            $verifyService->verify($arr['out_trade_no'], 'minipay', $arr, $arr['transaction_id'], $payerTotal);
        } catch (\Exception $e) {
            \think\facade\Log::error($e->getMessage());
            return json(['code' => 'FAIL','message' => $e->getMessage()]);
        }     
        return json(['code' => 'SUCCESS','message' => 'SUCCESS']);
    }

    public function miniBack()
    {
    }

    public function miniV3Back()
    {
    }
}